﻿using Host.InfrasIntructure.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Host.InfrasIntructure.Implement
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
    {
        private readonly HostDbContext db;
      

        public BaseRepository(HostDbContext db)
        {
            this.db = db;
           
        }
        public IQueryable<T> GetAddAsync(Expression<Func<T, bool>> expression)
        {
            return db.Set<T>().Where(expression).AsQueryable();
        }

        public IQueryable<T> GetAll()
        {
            return db.Set<T>();
        }
        /// <summary>
        /// 根据实体的主键ID异步获取实体。
        /// </summary>
        /// <param name="id">实体的主键ID。</param>
        /// <returns>返回实体，如果找不到则返回null。</returns>
        public async Task<T> GetByIdAsync(int id)
        {
            return await db.Set<T>().FindAsync(id);
        }
    }
}
